package cn.zyx.basic.service.impl;

import cn.zyx.basic.mapper.BaseMapper;
import cn.zyx.basic.query.BaseQuery;
import cn.zyx.basic.service.IBaseService;
import cn.zyx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础的service实现类，应该完成基础的增删改查实现
 */
// 不需要交给spring容器管理，应该交给子类继承，子类交给容器管理
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public T getById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.remove(id);
    }

    @Override
    public List<T> getAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery Query) {
        // 查询总条数
        Integer total = mapper.queryCount(Query);
        // 查询分页数据
        List<T> rows = mapper.queryData(Query);
        return new PageList<>(total,rows);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        mapper.patchDelete(ids);
    }
}
